package com.easepal802s.project.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.clj.fastble.data.BleDevice;
import com.easepal802s.project.CommondManager;
import com.easepal802s.project.R;
import com.easepal802s.project.interfaces.IProMasaChoose;
import com.easepal802s.project.observer.BleDataObserver;
import com.easepal802s.project.observer.CloseObserver;
import com.easepal802s.project.ui.presenter.ProfessionalMassagePresenter;
import com.easepal802s.project.ui.iview.IProfessionalMassageView;
import com.easepal802s.project.ble.BleController;
import com.easepal802s.project.utils.ActivityUtils;
import com.easepal802s.project.view.VerticalViewPager.VerticalAdapter;
import com.easepal802s.project.view.VerticalViewPager.VerticalViewPager;
import com.easepal802s.project.widget.LayoutNormalTitle;
import com.easepal802s.project.widget.LayoutPMFive;
import com.easepal802s.project.widget.LayoutPMFour;
import com.easepal802s.project.widget.LayoutPMOne;
import com.easepal802s.project.widget.LayoutPMThree;
import com.easepal802s.project.widget.LayoutPMTwo;
import com.ogawa.base.utils.CommonUtil;
import com.ogawa.base.utils.HexUtil;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import static com.easepal802s.project.ble.BleController.CONNECT_BREAK;

/**
 * 专业按摩
 */
public class ProfessionalMassageActivity extends BaseMvpTestActivity<ProfessionalMassagePresenter, ProfessionalMassageActivity> implements CloseObserver.OnCloseListerner,View.OnClickListener ,IProfessionalMassageView,IProMasaChoose,BleDataObserver.OnBleListerner{
   private LayoutPMOne mPageOne;
    private LayoutPMTwo mPageTwo;
    private LayoutPMThree mPageThree;
    private LayoutPMFour mPageFour;
    private LayoutPMFive mPageFive;
    private VerticalAdapter mAdapter;
    private VerticalViewPager mVerticalViewPager;
    private List<View> mVerticalViews;
    private ImageView mIvPointOne;
    private ImageView mIvPointTwo;
    private ImageView mIvPointThree;
    private ImageView mIvPointFour;
    private ImageView mIvPointFive;
    private TextView mTvNameOne;
    private TextView mTvNameTwo;
    private TextView mTvNameThree;
    private TextView mTvNameFour;
    private TextView mTvNameFive;
    private List<ImageView> mPoints = new ArrayList<>();
    private List<TextView> mClassifys = new ArrayList<>();
    private LayoutNormalTitle mLayoutTitle;
    private int mPage = -2;
    private int mPosition = -2;
    private MyHandler myHandler;
    private String mCommondCode;
    private String mCode;
    private String mName;
    private boolean isScanning;
    private boolean haReceive;



    @Override
    public ProfessionalMassagePresenter initPresenter() {
        return new ProfessionalMassagePresenter(this);
    }

    @Override
    protected void initCreateView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_professional_massage);
        BleDataObserver.getInst().registerBleDataObserver(this);
        CloseObserver.getInst().registerObserver(this);
        initView();
    }

    @Override
    public ProfessionalMassageActivity initActivity() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BleDataObserver.getInst().removeBleDataObserver(this);
        CloseObserver.getInst().removeObserver(this);
        mLayoutTitle.onDestory();
    }

    private void initView(){
        myHandler = new MyHandler(this);
        mVerticalViews = new ArrayList<>();
        mLayoutTitle = findViewById(R.id.activity_pro_mas_title);
        mLayoutTitle.setTitleNameGone();
        mPageOne =(LayoutPMOne)LayoutInflater.from(this).inflate(R.layout.layout_pm_page_one,null);
        mPageTwo =(LayoutPMTwo) LayoutInflater.from(this).inflate(R.layout.layout_pm_page_two,null);
        mPageThree =(LayoutPMThree) LayoutInflater.from(this).inflate(R.layout.layout_pm_page_three,null);
        mPageFour =(LayoutPMFour) LayoutInflater.from(this).inflate(R.layout.layout_pm_page_four,null);
        mPageFive =(LayoutPMFive) LayoutInflater.from(this).inflate(R.layout.layout_pm_page_five,null);
        mTvNameOne = findViewById(R.id.tv_name1);
        mTvNameTwo = findViewById(R.id.tv_name2);
        mTvNameThree = findViewById(R.id.tv_name3);
        mTvNameFour = findViewById(R.id.tv_name4);
        mTvNameFive = findViewById(R.id.tv_name5);
        mTvNameOne.setOnClickListener(this);
        mTvNameTwo.setOnClickListener(this);
        mTvNameThree.setOnClickListener(this);
        mTvNameFour.setOnClickListener(this);
        mTvNameFive.setOnClickListener(this);
        mPageOne.setAction(this);
        mPageTwo.setAction(this);
        mPageThree.setAction(this);
        mPageFour.setAction(this);
        mPageFive.setAction(this);

        mIvPointOne = findViewById(R.id.pm_potin_one);
        mIvPointOne.setSelected(true);
        mTvNameOne.setSelected(true);
        mIvPointTwo = findViewById(R.id.pm_potin_two);
        mIvPointThree = findViewById(R.id.pm_potin_three);
        mIvPointFour = findViewById(R.id.pm_potin_four);
        mIvPointFive = findViewById(R.id.pm_potin_five);
        mPoints.clear();
        mPoints.add(mIvPointOne);
        mPoints.add(mIvPointTwo);
        mPoints.add(mIvPointThree);
        mPoints.add(mIvPointFour);
        mPoints.add(mIvPointFive);
        mClassifys.clear();
        mClassifys.add(mTvNameOne);
        mClassifys.add(mTvNameTwo);
        mClassifys.add(mTvNameThree);
        mClassifys.add(mTvNameFour);
        mClassifys.add(mTvNameFive);
        mVerticalViewPager = findViewById(R.id.verticalViewPager);
     mVerticalViews.add(mPageOne);
     mVerticalViews.add(mPageTwo);
     mVerticalViews.add(mPageThree);
     mVerticalViews.add(mPageFour);
     mVerticalViews.add(mPageFive);
     mVerticalViewPager.setOnPageChangeListener(new OnPageChangeListenerImpl());
     mAdapter = new VerticalAdapter(mVerticalViews);
     mVerticalViewPager.setAdapter(mAdapter);
    }

    @Override
    public void chooseMassage(String name, String Code) {
        Log.e("chooseMassage",mCommondCode+"===="+BleController.getInst().getIsScanning()+"=="+Code);
        if(!TextUtils.isEmpty(mCommondCode)&&!mCommondCode.equals(Code)){
            CommondManager.getInst().startProgramEvent(HexUtil.hexToInt(Code));
            if(!TextUtils.isEmpty(mCommondCode)){
                CommondManager.getInst().endProgramEvent(HexUtil.hexToInt(mCommondCode));
            }

            BleController.getInst().sendSingleData(Code,true);
        }
        ActivityUtils.startProMasDetailActivity(this, name, Code);


        }

    @Override
    public void onConnectState(int state) {

    }

    @Override
    public void onTargetDeviceFind(BleDevice bleDevice) {

    }

    @Override
    public void onStarScan() {

    }

    @Override
    public void onNoDeviceFind() {

    }

    @Override
    public void onGetData(String data) {
        mBasePresenter.getDatas(data);
    }

    @Override
    public void onPowerOff() {
        if(BleController.getInst().getConnectState()==CONNECT_BREAK){
            ActivityUtils.startHomeActivity(this,"BREAK");
        }
        else {
            ActivityUtils.startHomeActivity(this,"none");
        }
        this.finish();
    }

    @Override
    public void onDataChange(String bage, int auto, int skill, int air,String name,String commond) {

    }

    @Override
    public void onShoulderAdjust(int data) {
        if(CommonUtil.isActivityTop(this.getClass(),this)&&data==1){
            ActivityUtils.startShouldersAdjustmentActivity(this);
        }
    }


    @Override
    public void onClose() {
        this.finish();
    }


    class OnPageChangeListenerImpl implements VerticalViewPager.OnPageChangeListener {

  @Override
  public void onPageScrolled(int position, float positionOffset,
                             int positionOffsetPixels) {

  }

  @Override
  public void onPageSelected(int position) {
      for(int i = 0;i<mPoints.size();i++){
          if(position==i){
              mPoints.get(i).setSelected(true);
              mClassifys.get(i).setSelected(true);
          }
          else {
              mPoints.get(i).setSelected(false);
              mClassifys.get(i).setSelected(false);
          }
      }

  }

  @Override
  public void onPageScrollStateChanged(int state) {

  }
 }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_name1:

                mVerticalViewPager.setCurrentItem(0);
                break;
            case R.id.tv_name2:
                mVerticalViewPager.setCurrentItem(1);
                break;
            case R.id.tv_name3:
                mVerticalViewPager.setCurrentItem(2);
                break;
            case R.id.tv_name4:
                mVerticalViewPager.setCurrentItem(3);
                break;
            case R.id.tv_name5:
                mVerticalViewPager.setCurrentItem(4);
                break;

        }

    }


    @Override
    public void getData(int page, int position,String code) {
        mCommondCode = code;
        myHandler.obtainMessage(100,page,position).sendToTarget();


    }



    private void makaChoose(int page,int position){
        mPage = page;
        mPosition = position;
        Log.e("getDataPer",page+"==="+position);
        mPosition = position;
        mPage     = page;

        if(page==0){
            mPageOne.setSelect(position);
            mPageTwo.setSelect(-1);
            mPageThree.setSelect(-1);
            mPageFour.setSelect(-1);
            mPageFive.setSelect(-1);
        }
        else if(page==1){
            mPageTwo.setSelect(position);
              mPageOne.setSelect(-1);
            mPageThree.setSelect(-1);
            mPageFour.setSelect(-1);
            mPageFive.setSelect(-1);
        }
        else  if(page==2){
               mPageTwo.setSelect(-1);
              mPageOne.setSelect(-1);
            mPageThree.setSelect(position);
            mPageFour.setSelect(-1);
            mPageFive.setSelect(-1);

        }
        else if(page==3){
               mPageTwo.setSelect(-1);
              mPageOne.setSelect(-1);
            mPageThree.setSelect(-1);
            mPageFour.setSelect(position);
            mPageFive.setSelect(-1);

        }
        else if(page==4){
                mPageTwo.setSelect(-1);
              mPageOne.setSelect(-1);
            mPageThree.setSelect(-1);
            mPageFour.setSelect(-1);
            mPageFive.setSelect(position);

        }
        else {
            mPageOne.setSelect(-1);
            mPageTwo.setSelect(-1);
            mPageThree.setSelect(-1);
            mPageFour.setSelect(-1);
            mPageFive.setSelect(-1);
        }


    }

    private  class MyHandler extends Handler {
        WeakReference<ProfessionalMassageActivity> mMainActivityWeakReference;

        MyHandler(ProfessionalMassageActivity activity) {
            mMainActivityWeakReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            final ProfessionalMassageActivity mActivity = mMainActivityWeakReference.get();
            int page = msg.arg1;
            int position = msg.arg2;
            if (mActivity != null) {

                makaChoose(page,position);
            }
        }
    }






}
